var Module = function () {

    this.$tree = $('#tree');
    this.$grid_selector = $("#grid-table");
    this.pager_selector = "#grid-pager";

    this.URL = {
        GET_BY_PARENT_ID: '/member/module/get/all/parentId',
        MODULE_EDIT: '/member/module/modify'
    };

    this.colModel = [
        {name: 'moduleId', key: true, width: 50, editable: true},
        {
            name: 'parentId',
            width: 80,
            hidden: true,
            editable: true,
            addHiddenControl: function () {
                // 当前父id
                return $(this).xumf_jqGird('getGridParam', 'postData')['searchString'];
            },
            editrules: {required: true},
            editoptions: {disabled: true}
        }, {
            name: 'name',
            width: 80,
            editable: true,
            editoptions: {size: "20", maxlength: "30"},
            editrules: {required: true}
        },
        {
            name: 'code',
            editable: true,
            editoptions: {size: "20", maxlength: "30"},
            editrules: {required: true}
        },
        {
            name: 'icon',
            editable: true,
            editoptions: {size: "20", maxlength: "30"}
        },
        {
            name: 'url',
            sortable: false,
            editable: true,
            editoptions: {size: "20", maxlength: "500"}
        },
        {
            name: 'status',
            width: 70,
            sortable: false,
            editable: true,
            edittype: "checkbox",
            editoptions: {
                value: "1:0",
                defaultValue: '1'
            },
            editrules: {required: true},
            formatter: function (cellValue) {
                if (cellValue === '1' || cellValue === 1)
                    return '开启';
                else
                    return '关闭';
            },
            unformat: function (cellValue) {
                if (cellValue === '开启')
                    return '1';
                else
                    return '0';
            }
        },
        {
            name: 'sequence',
            width: 50,
            sortable: true,
            editable: true,
            editoptions: {size: "20", maxlength: "30"},
            editrules: {required: true, number: true}
        },
        {
            name: 'comment',
            sortable: false,
            editable: true,
            edittype: "textarea",
            editoptions: {size: "20", maxlength: "30"},
            editrules: {required: true}
        }, {name: 'createdAt', index: 'createdAt', width: 100}
    ];

    this.options = {
        url: this.URL.GET_BY_PARENT_ID,
        editurl: this.URL.MODULE_EDIT,
        postData: {searchString: $.Const.ROOT_ID},
        pager: this.pager_selector,
        sortname: 'sequence',
        caption: "模块列表",
        colNames: ['ID', '父ID', '名称', '编号', '图标', 'URL', '状态', '顺序', '说明', '创建时间'],
        colModel: this.colModel

    };
};

Module.prototype = {
    remoteDataSource: function (options, callback) {
        if (!options.hasOwnProperty('dataAttributes')) { // 初始化树形时间，添加root菜单
            callback({
                data: [{name: '根模块', type: 'folder', dataAttributes: {id: $.Const.ROOT_ID}}]
            });
        } else {
            var id = options.dataAttributes.id;
            $.get('/member/module/tree/get/' + id).done(function (res) {
                callback({data: res.data});
            });
        }
    },
    initTree: function () {
        var $grid = this.$grid_selector;
        /**
         * 树形
         */
        this.$tree.xumf_tree({dataSource: this.remoteDataSource});
        // 默认打开顶级菜单
        this.$tree.xumf_tree('openFolder', this.$tree.find('#' + $.Const.ROOT_ID));
        // 树形点击事件
        this.$tree.on('selected.fu.tree', function (event, data) {
            $grid.setGridParam({postData: {searchString: data.target.dataAttributes.id}}).trigger('reloadGrid');
        });
    },
    initGird: function () {
        var $tree = this.$tree;
        this.$grid_selector.xumf_jqGird(this.options);
        // 更新tree
        this.$grid_selector.on('xumf_gird.edit.afterComplete', function (event, data, postData) {
            $tree.tree('refreshFolder', $tree.find('#' + postData.parentId));
        });
        this.$grid_selector.on('xumf_gird.delete.afterComplete', function (event, data, postData) {
            var selectedItem = $tree.xumf_tree.getSelectedItem($tree);
            $tree.tree('refreshFolder', $tree.find('#' + selectedItem.moduleId || '0'));
        });
        this.$grid_selector.on('xumf_gird.add.afterComplete', function (event, data, postData) {
            $tree.tree('refreshFolder', $tree.find('#' + postData.parentId));
        });
    }
};


